import { NavigationActions } from 'react-navigation';

import { router } from '../navigations';
import {
  GO_BACK,
  LOGIN,
  LOGOUT,
  TO_NEW_TWEET_SCREEN,
  TO_LOGIN_SCREEN,
  TO_REGISTER_SCREEN,
  TO_TWEETS_LIST_VIEW
} from '../types';

export default (state, action) => {
  let newState;
  switch (action.type) {
    case GO_BACK:
      newState = router.getStateForAction(
        NavigationActions.back(),
        state
      );
      break;
    case LOGIN:
      if (action.needBack) {
        newState = router.getStateForAction(
          NavigationActions.back(),
          state
        );
      }
      break;
    case LOGOUT:
      newState = router.getStateForAction(
        NavigationActions.navigate({ routeName: 'Login' }),
        state
      );
      break;
    case TO_TWEETS_LIST_VIEW:
      newState = router.getStateForAction(
        NavigationActions.navigate({ routeName: 'Home' }),
        state
      );
      break;
    case TO_NEW_TWEET_SCREEN:
      newState = router.getStateForAction(
        NavigationActions.navigate({ routeName: 'NewTweet' }),
        state
      );
      break;
    case TO_LOGIN_SCREEN:
      newState = router.getStateForAction(
        NavigationActions.navigate({ routeName: 'Login' }),
        state
      );
      break;
    case TO_REGISTER_SCREEN:
      newState = router.getStateForAction(
        NavigationActions.navigate({ routeName: 'Register' }),
        state
      );
      break;
    default: newState = router.getStateForAction(action, state);
  }
  return newState || state;
};
